package com.wyiot.pet.controller;

import com.wyiot.core.exception.ValidateException;
import com.wyiot.pet.model.entity.OrderDO;
import com.wyiot.pet.model.vo.WaybillDetailVO;
import com.wyiot.core.security.UserContext;
import com.wyiot.pet.service.WayBillService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 运单管理控制层
 *
 * @author yly
 * @since 2022年04月06日
 */
@RestController
public class WayBillController {
    @Resource
    WayBillService wayBillService;

    /**
     * 查看运单详情
     *
     * @param waybillId 运单id
     */
    @GetMapping("/waybill/{id}")
    public WaybillDetailVO queryWaybillByWaybillId(@PathVariable("id") Long waybillId) {
        /*如果运单所属用户不为登入用户或者 订单收件电话不为登入用户电话则查询失败*/
        if (waybillId == null){
            throw ValidateException.of("请输入运单号");
        }
        OrderDO orderDO = wayBillService.queryOrderByWaybillId(waybillId);
        if (orderDO != null && (orderDO.getUserId().equals(UserContext.getUserId()) || orderDO.getReceiverPhone().equals(UserContext.getUserPhone()))){
            return wayBillService.queryWaybillById(waybillId);
        }
        throw ValidateException.of("查询失败");
    }
}
